package com.ee.chat.api.config;

import com.aliyun.openservices.ons.api.PropertyValueConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.ee.chat.api.listener.mq.CallFreeMQListener;
import com.ee.chat.api.listener.mq.WordMQListener;
import com.ee.chat.common.serconfig.MyNeteaseApp;
import com.ee.chat.common.server.mq.MQServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class MQConfig {
    @Value("${ali.rocketmq.call.topic}")
    String call_topic;
    @Value("${ali.rocketmq.call.tag}")
    String call_tag;
    @Value("${ali.rocketmq.call.groupId}")
    String call_groupId;

    @Value("${ali.rocketmq.word.groupId}")
    String word_groupId;
    @Value("${ali.rocketmq.word.topic}")
    String word_topic;
    @Value("${ali.rocketmq.word.tag1}")
    String wordTag1;
    @Value("${ali.rocketmq.word.tag2}")
    String wordTag2;

    @Resource
    MQServer mqServer;
    @Resource
    CallFreeMQListener callFreeMQListener;
    @Resource
    WordMQListener wordMQListener;
    @Resource
    MyNeteaseApp app;

    //音视频通话MQ消费者，集群消费
    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean ClusteringMQConsumer(){
        List<Map<String, Object>> messages = new ArrayList<>();

        //音视频通话收费消息
        Map<String, Object> callFree = new HashMap<>();
        callFree.put("topic", call_topic);
        callFree.put("tag", app.getIdPrefix() + "-" + call_tag);
        callFree.put("messageListener", callFreeMQListener);
        messages.add(callFree);

        ConsumerBean consumerBean = mqServer.buildConsumer(call_groupId, PropertyValueConst.CLUSTERING, messages);
        return consumerBean;
    }

    //词匹配MQ消费者，广播消费
    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean BroadcastingMQConsumer(){
        List<Map<String, Object>> messages = new ArrayList<>();

        //广播词库更新
        Map<String, Object> map = new HashMap<>();
        map.put("topic", word_topic);
        map.put("tag", app.getIdPrefix() + "-" + wordTag1 + "||" + app.getIdPrefix() + "-" + wordTag2);
        map.put("messageListener", wordMQListener);
        messages.add(map);

        ConsumerBean consumerBean = mqServer.buildConsumer(word_groupId, PropertyValueConst.BROADCASTING, messages);
        return consumerBean;
    }


}
